package com.ycyoes.demos.basic.reference;

public class StrongReferenceTest {
	public static int M = 1024 * 1024;
	
	public static void printMemory(String tag) {
		Runtime runtime = Runtime.getRuntime();
		int M = StrongReferenceTest.M;
		System.out.println("\n" + tag + ":");
		System.out.println(runtime.freeMemory() / M + "M(free)/" + runtime.totalMemory() / M + "M(total)");
	}
	
	public static void main(String[] args) {
		StrongReferenceTest.printMemory("1.原可用内存和总内存");
		
		//实例化10M数组并与strongReference建立强引用
		byte[] strongReference = new byte[10 * StrongReferenceTest.M];
		StrongReferenceTest.printMemory("2.实例化10M的数组，并建立强引用");
		System.out.println("strongReference: " + strongReference);
		
		System.gc();
		StrongReferenceTest.printMemory("3.GC后");
		System.out.println("strongReference: " + strongReference);
		
		//strongReference = null; 后，强引用断开了
		strongReference = null;
		StrongReferenceTest.printMemory("4.强引用断开后");
		System.out.println("strongReference: " + strongReference);
		
		System.gc();
		StrongReferenceTest.printMemory("5.GC后");
		System.out.println("strongReference: " + strongReference);
	}
}
